package org.mogu;

import java.util.Date;

import org.json.JSONObject;
import org.mogu.control.model.IMUser;
import org.mogu.db.DBhelper;
import org.mogu.service.InfoSync;
import org.mogu.staticvalue.Convert;
import org.mogu.tools.UserUtil;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Window;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.view.View;

/*
 *
 * 联系人管理界面
 * @author androidSource@139.com
 * */
public class ContactManagerActivity extends Activity implements
		View.OnClickListener {

	private Handler mMainHandler;

	/*
	 * 相关变量定义
	 */
	private RelativeLayout openConversation = null;
	private RelativeLayout detailInfoItem = null;
	private RelativeLayout viewChatHistoryItem = null;
	private RelativeLayout viewDeleteFriendItem = null;
	private RelativeLayout change_friend_group_item = null;
	private RelativeLayout delete_contactItem = null;
	private RelativeLayout viewDelHistoryItem = null;


	private TextView title = null;
	private TextView signature = null;
	private IMUser imUser;

	private View view2 = null;
	private View view3 = null;
	private View view4 = null;
	Intent intent;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);// 启用窗体的扩展特性,此处请求为没有标题的activity
		/*
		 * 获取当前好友数据
		 */
		// TreeNode mUser = QQApplication.instance.getUser();
		intent = this.getIntent();
		Bundle bunde = intent.getExtras();
		if (bunde != null) {
			imUser = (IMUser) bunde.getSerializable("iMUser");
		}
		
		setContentView(R.layout.friend_manager);// 设置当前使用的布局文件


		/*
		 * 控件相关初始化
		 */
		title = (TextView) findViewById(R.id.title);
		signature = (TextView) findViewById(R.id.signature);

		
		// 设置昵称
		title.setText(imUser.getUnicename());
		// 设置当前心情
		signature.setText(imUser.getDesc());

		/*
		 * 获取相应控件，并设置控件单击的事件监听器
		 */
		// changeAlterName =
		// (RelativeLayout)findViewById(R.id.changeAlterNameItem);
		// changeAlterName.setOnClickListener(this);

		openConversation = (RelativeLayout) findViewById(R.id.sendMsgItem);
		openConversation.setOnClickListener(this);

		detailInfoItem = (RelativeLayout) findViewById(R.id.detailInfoItem);
		detailInfoItem.setOnClickListener(this);

		viewChatHistoryItem = (RelativeLayout) findViewById(R.id.viewChatHistoryItem);
		viewChatHistoryItem.setOnClickListener(this);

		 viewDeleteFriendItem = (RelativeLayout)findViewById(R.id.delete_frienditem);
		 viewDeleteFriendItem.setOnClickListener(this);

		// view_send_mail = (RelativeLayout)findViewById(R.id.view_send_mail);
		// view_send_mail.setOnClickListener(this);

		change_friend_group_item = (RelativeLayout) findViewById(R.id.change_friend_group_item);
		// change_friend_group =
		// (TextView)findViewById(R.id.change_friend_group);
		change_friend_group_item.setOnClickListener(this);
		view2 = (View) findViewById(R.id.divder2);
		view3 = (View) findViewById(R.id.divder3);
		view4 = (View) findViewById(R.id.divder4);
		// change_friend_group_item.setBackgroundResource(R.drawable.opt_selectitem_bg);

		delete_contactItem = (RelativeLayout) findViewById(R.id.delete_contactItem);
		delete_contactItem.setOnClickListener(this);

		viewDelHistoryItem = (RelativeLayout) findViewById(R.id.viewDelHistoryItem);
		viewDelHistoryItem.setOnClickListener(this);
		mMainHandler = new Handler() {

			@Override
			public void handleMessage(Message msg) {

				// // 接收子线程的消息
				if (msg.arg1 == 1) {
					changeStatus();
				}
				if(msg.arg1==2){
					onBack(null);
				}
			}

		};

		changeStatus();
	}

	public void addStranger() {
		if (imUser.getTitle() == null || "".equals(imUser.getTitle())) {

			JSONObject user = new JSONObject();
			UserUtil.getJsonbyImUser(user, imUser);
//			imUser.setTitle("2");
			ContentValues activeuser = new ContentValues();
			activeuser.put("maincode", imUser.getMaincode());
			activeuser.put("content", user.toString());

			activeuser.put("status", "1");
			activeuser.put("level", "101");
			activeuser.put("father", imUser.getMaincode() + "-s1");
			activeuser.put("replyType", "30");
			activeuser
					.put("lastUpdateTime", Convert.format1.format(new Date()));
			String[] where = { imUser.getCode(), Convert.uname };
			synchronized (InfoSync.class) {
				int num = DBhelper
						.getDBHelper(this)
						.getWritableDatabase()
						.update("content", activeuser, "code=? and username=?",
								where);
				if (num == 0) {
					activeuser.put("code", imUser.getCode());
					activeuser.put("username", Convert.uname);
					activeuser.put("title", "2");
					imUser.setTitle("2");
					DBhelper.getDBHelper(this).getWritableDatabase()
							.insert("content", null, activeuser);
				}

			}
		}
	}

	public void onBack(View view) {
		if (intent == null) {
			return;
		}
		this.setResult(RESULT_OK, intent);
		this.finish();

	}

	public void changeStatus() {
		if ("1".equals(imUser.getTitle())) {
			change_friend_group_item.setVisibility(View.GONE);
			view3.setVisibility(View.GONE);
			delete_contactItem.setVisibility(View.VISIBLE);
			view4.setVisibility(View.VISIBLE);
			viewDelHistoryItem.setVisibility(View.VISIBLE);
			viewChatHistoryItem.setVisibility(View.VISIBLE);
			view2.setVisibility(View.VISIBLE);
		}
		if ("2".equals(imUser.getTitle())) {
			change_friend_group_item.setVisibility(View.VISIBLE);
			view3.setVisibility(View.VISIBLE);
			delete_contactItem.setVisibility(View.GONE);
			view4.setVisibility(View.GONE);
			viewDelHistoryItem.setVisibility(View.VISIBLE);
			viewChatHistoryItem.setVisibility(View.VISIBLE);
			view2.setVisibility(View.VISIBLE);
		}
		if (imUser.getTitle() == null || "".equals(imUser.getTitle())) {
			change_friend_group_item.setVisibility(View.VISIBLE);
			view3.setVisibility(View.VISIBLE);
			delete_contactItem.setVisibility(View.GONE);
			view4.setVisibility(View.GONE);
			viewDelHistoryItem.setVisibility(View.GONE);
			viewChatHistoryItem.setVisibility(View.GONE);
			view2.setVisibility(View.GONE);
			
		}
	}

	public void goChat() {
		Intent mIntent = new Intent(this, ChatWindowsActivity.class);
		// mIntent.putExtras(bundle);
		Bundle bundle = new Bundle();
		bundle.putSerializable("iMUser", imUser);
		mIntent.putExtras(bundle);
//		mIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
		startActivityForResult(mIntent, 0);
	}

	public void onHistory() {
		Intent mIntent = new Intent(this, ChatHistoryActivity.class);
		// mIntent.putExtras(bundle);
		Bundle bundle = new Bundle();
		bundle.putSerializable("iMUser", imUser);
		mIntent.putExtras(bundle);
//		 mIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
		startActivityForResult(mIntent, 0);
	}
	 public void onUpdateInfo(View view){
	    	Intent intent = new Intent(this, QQ_Info.class);
	    	Bundle bundle = new Bundle();
			bundle.putSerializable("iMUser",imUser);
			bundle.putString("maincode",imUser.getMaincode());
			bundle.putString("code",imUser.getFather());
			intent.putExtras(bundle);
//			intent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
	    	startActivityForResult(intent, 0);
	    }
	 public void onFriendManage(){
		 Intent mIntent = new Intent(this, QQ_Info.class);
			Bundle bundle = new Bundle();
			bundle.putSerializable("iMUser", imUser);
			bundle.putString("maincode",imUser.getMaincode());
			bundle.putString("code",imUser.getCode());
			mIntent.putExtras(bundle);
//			mIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
			startActivityForResult(mIntent,0);
	 }

	/*
	 * 控件单击事件监听器接口函数
	 */
	@Override
	public void onClick(View view) {

		/*
		 * 根据id来判断当前点击的具体按钮
		 */
		switch (view.getId()) {
		// //修改昵称
		// case R.id.changeAlterNameItem:
		// SettingManagerActivity.ChangeAlertNameDialog(ContactManagerActivity.this);
		// break;

		// 发起聊天
		case R.id.sendMsgItem:
			addStranger();
			goChat();
			// Intent mIntent = new Intent(this, ChatWindowsActivity.class);
			// mIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
			// startActivityForResult(mIntent, 0);
			break;

		// 查看详细资料
		case R.id.detailInfoItem:
			onFriendManage();
			// Intent intent = new Intent(Intent.ACTION_VIEW);
			// intent.setData(Uri.parse("http://fwd.3g.qq.com:8080/forward.jsp?bid=481"));
			// instance.startActivity(intent);
			break;

		// 查看聊天记录
		case R.id.viewChatHistoryItem:
			onHistory();
			// Intent vIntent = new Intent(this, ChatHistoryActivity.class);
			// vIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
			// instance.startActivity(vIntent);
			break;

		// 查看qq空间
		// case R.id.viewQzoneItem:
		// Intent qIntent = new Intent(Intent.ACTION_VIEW);
		// qIntent.setData(Uri.parse("http://fwd.3g.qq.com:8080/forward.jsp?bid=186"));
		// instance.startActivity(qIntent);
		// break;

		// 加为好友
		case R.id.change_friend_group_item:
			SettingManagerActivity.AddContactItem(ContactManagerActivity.this,
					imUser, mMainHandler);
			// Intent sIntent = new Intent(Intent.ACTION_VIEW);
			// sIntent.setData(Uri.parse("http://fwd.3g.qq.com:8080/forward.jsp?bid=187"));
			// instance.startActivity(sIntent);
			break;

		// 删除好友
		case R.id.delete_contactItem:
			SettingManagerActivity.DeleteContactItem(
					ContactManagerActivity.this, imUser, mMainHandler);
			break;
			// 删除好友
		case R.id.delete_frienditem:
			SettingManagerActivity.DeleteFriendItem(
					ContactManagerActivity.this, imUser, mMainHandler);
			break;

		// 删除聊天记录
		case R.id.viewDelHistoryItem:
			SettingManagerActivity.DeleteHistoryItem(
					ContactManagerActivity.this, imUser.getCode());
			break;
		}
	}

}